package netcom;

import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
import pimp.ApplicationError;
import pimp.GFX;

class Client
{
    Socket server;
    String server_addr;
    int server_port;
    Protocol protocol;
    private static final String SENDING_FILE = "SENDING_FILE";
    private static final String LOGIN = "LOGIN";
    private static final String OK = "OK";
    private static final String WRONG = "WRONG";
    private static final String GET_FILE = "GET_FILE";
    String user;
    String pass;

    Client(String _user, String _pass)
    {
        server_addr = "localhost";
        server_port = 5000;
        server = null;
        protocol = new Protocol();
        user = _user;
        pass = _pass;
        //connect();
    }
    Client(String _user, String _pass, String server_addr, int port)
    {
        this.server_addr = server_addr;
        server_port = port;
        server = null;
        protocol = new Protocol();
        user = _user;
        pass = _pass;
        //connect();
    }
    public String getServer()
    {
        return server_addr;
    }
    public int getPort()
    {
        return server_port;
    }
    public void changeServer(String server_addr, int server_port)
    {
        clean();
        this.server_addr = server_addr;
        this.server_port = server_port;
        //connect();
    }

    public boolean connect()
    {
        System.out.println("Connecting to server");
        try
        {
            server = new Socket(server_addr, server_port);
        }
        catch (UnknownHostException e)
        {
            ApplicationError.showError("Unknown host: " + server_addr);
            System.out.println("Unknown host: " + server_addr);
            return false;
        }
        catch(IOException e)
        {
            ApplicationError.showError("Could not connect to the server");
            System.out.println("Could not connect to the server");
            return false;
        }
        protocol.negotiateWithServer(server);

        int logins = 3;
        String answer = null;
        while(logins-- > 0)
        {
            answer = login();
            if(answer.compareTo(OK) == 0)
            {
                System.out.println("Logged in");
                return true;
            }
            if(answer.compareTo(WRONG) == 0 && logins != 0)
            {
                ApplicationError.showError("Incorrect login, please try again.\n" +
                            "                   Remaining tries: " + logins);
            }
        }
        return false;
    }

    private String login()
    {
        System.out.println("Logging in...");
        String got_user = JOptionPane.showInputDialog("Enter login: ");
        String got_pass = JOptionPane.showInputDialog("Enter password: ");
        protocol.sendMsg(LOGIN);
        protocol.sendMsg(got_user);
        protocol.sendMsg(got_pass);
        return protocol.getMsg();
    }

    public void saveData(String data)
    {
        System.out.println("Sending data");
        protocol.sendMsg(SENDING_FILE);
        protocol.sendMsg(data);
    }

    public String getData()
    {
        System.out.println("Getting data");
        protocol.sendMsg(GET_FILE);
        return protocol.getMsg();
    }
    public void close()
    {
        protocol.close();
    }

    protected void clean()
    {
        try
        {
            server.close();
        }
        catch(IOException e)
        {
            System.out.println("Could not close socket");
            return;
        }
    }
}

